package validator;

import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

import command.MassiveImportForm;

@Component
public class MassiveImportFormValidator implements Validator {

	private static final long MAX_FILE_SIZE = 1048576L;
	
	@Override
	public boolean supports(Class<?> clazz) {
		return MassiveImportForm.class.equals(clazz);
	}
	
	@Override
	public void validate(Object target, Errors errors) {
		MassiveImportForm form = (MassiveImportForm) target;
		if (form.getFilename() != null	&& !form.getFilename().isEmpty()) {
//			if (form.getFilename().getContentType() != null) {
//				if (!form.getFilename().getContentType().contains("image")) {
//					errors.rejectValue("image", "type");
//				}
//			}
			if (form.getFilename().getSize() > MAX_FILE_SIZE) {
				errors.rejectValue("filename", "size");
			}
		}	
	}
}